<script src="../../router/index.js"></script>
<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin/brand/BrandListView' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>品牌管理</el-breadcrumb-item>
    </el-breadcrumb>
    <h3 style="margin: 20px 0;">品牌详情</h3>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="150" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="200" align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键词" align="center"></el-table-column>
      <el-table-column prop="description" label="内容描述" width="280" align="center"></el-table-column>
      <el-table-column prop="enable" label="是否启用" width="200" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="handleChangeEnable(scope.$index, scope.row.id, scope.row.enable)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button circle icon="el-icon-edit" size="mini" @click="handleEdit(scope.$index,scope.row)"></el-button>
          <el-dialog
              title="编辑品牌"
              :visible.sync="dialogVisible"
              width="50%"
              >
            <div style="margin: 20px;"></div>
            <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
              <el-form-item label="品牌ID">
                <el-input v-model="formLabelAlign.id" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="品牌名称">
                <el-input v-model="formLabelAlign.name"></el-input>
              </el-form-item>
              <el-form-item label="品牌logo">
                <el-input v-model="formLabelAlign.logo"></el-input>
              </el-form-item>
              <el-form-item label="关键词">
                <el-input v-model="formLabelAlign.keywords"></el-input>
              </el-form-item>
              <el-form-item label="品牌概述">
                <el-input v-model="formLabelAlign.description"></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="confirm">确 定</el-button>
  </span>
          </el-dialog>
          <el-button circle icon="el-icon-delete" size="mini" type="danger"
                     @click="openDeleteConfirm(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import Common from "@/views/common/Common"

export default {
  data() {
    return {
      tableData: [],
      brandDetails: [],
      dialogVisible: false,
      // dialogVisibles: false,
      labelPosition: 'right',
      formLabelAlign: {
        id:'',
        name: '',
        logo: '',
        description: '',
        keywords: ''
      },
    }
  },
  methods: {
    openDeleteConfirm(id) {
      this.$confirm('此操作将永久删除该品牌, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(id);
      }).catch(() => {
      });
    },
    handleEdit(index , row) {
      // this.userIndex = index;
      // this.formLabelAlign = {
      //   id: row.id,
      //   name: row.name,
      //   logo: row.logo,
      //   description: row.description,
      //   keywords: row.keywords,
      // };
      // this.dialogVisible = true;
    },

    confirm(id) {//确认修改
      var that = this
      //网络请求获取数据axios
      let url = Common.data.BASEURI+'/goods/brands/brandList/'+id+'/update';
      this.axios
          .create({headers: {'Authorization': 'Bearer ' + localStorage.getItem('jwt')}})
      .post(url,this.formLabelAlign).then((response) => {
        let json = response.data;
        console.log(json)
        if (json.state == 200) {
          this.$message({
            message: '修改品牌成功！',
            type: 'success'
          });
          this.loadBrands()
          this.dialogVisible = false;
        } else {
          this.$message.error(response.data.message);
        }
      })
    },
    handleDelete(id) {
      console.log('将删除id=' + id + "的品牌数据……");
      let url = Common.data.BASEURI+'/goods/brands/' + id + '/delete';
      this.axios
          .create({headers: {'Authorization': 'Bearer ' + localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let json = response.data;
        if (json.state == 200) {
          this.$message({
            message: '删除品牌成功！',
            type: 'success'
          });
        } else {
          this.$message.error(response.data.message);
        }
        this.loadBrands();
      })
    },
    handleChangeEnable(i, id, toState) {
      // let enableText = ['禁用', '启用'];
      // let originState = (toState + 1) % 2;
      // this.$message.error('尝试将id=' + id + '的品牌状态从【' + enableText[originState] + '】改为【' + enableText[toState] + '】，但是，此功能尚未实现！');
      // setTimeout(() => {
      //   this.tableData[i].enable = originState;
      // }, 200);
    },
    loadBrands: function () {
      console.log('loadBrands()');
      let url = Common.data.BASEURI+'/goods/brands/brandList';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': "Bearer " + localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let json = response.data;
        if (json.state == 200) {
          // let list = json.data;
          // for (let i = 0; i < list.length; i++) {
          //   console.log(list[i]);
          // }
          this.tableData = json.data;
        } else {
          this.$message.error(response.data.message);
        }
      });
    }
  },
  created() {
    // created >>> 已创建，将在mounted、显示页面之前执行
    console.log('vue created');
  },
  mounted() {
    // mounted >>> 已挂载，将在created之后、显示页面之前执行
    console.log('vue mounted');
    this.loadBrands();
  }
}
</script>

<style scoped>

</style>
